package model;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;


public class Player {
	String name;
	int account;
	Image pawn;
	Cell position;//position of the player
	int free_jail;//number of card free jail
	int try_espape;//number of try to make a double if prisoner
	int double_done;//number of double performed
	boolean prisoner;//boolean to know if the player is a prisoner
	boolean bot;
	ArrayList<Card>card_owned;

	public Player (String name, ImageIcon pawn){
		this.name=name;
		if(pawn!=null){
			this.pawn=pawn.getImage();
		}
		prisoner=false;
		free_jail=0;
		try_espape=0;
		double_done=0;
		card_owned=new ArrayList<Card>();
	}

	public void setBot(){
		bot=true;
	}
	
	public boolean isBot(){
		return bot;
	}
	
	public String getName(){
		return name;
	}

	public int getFree_jail(){
		return free_jail;
	}

	public int getAccountBalance(){
		return account;
	}

	public Cell getPosition(){
		return position;
	}

	public Image getPawn(){
		return pawn;
	}

	public boolean getPrisonerStatut(){
		return prisoner;
	}

	public int getTryEscape(){
		return try_espape;
	}

	public int getDoublePerformed(){
		return double_done;
	}

	//get a card with the name of the place
	public Card getCard(String name){
		for(int i=0;i<card_owned.size();i++){
			if(card_owned.get(i).name.equals(name)){
				return card_owned.get(i);
			}
		}
		return null;
	}


	//return families which are owned
	public ArrayList<String> have_full_family(){
		ArrayList<String> fullFamily=new ArrayList<String>();
		int nbCardPink=0;
		int nbCardBlue=0;
		int nbCardOrange=0;
		int nbCardRed=0;
		int nbCardCyan=0;
		int nbCardMagenta=0;
		int nbCardYellow=0;
		int nbCardGreen=0;
		String[]family={"pink","cyan","magenta","orange","red","yellow","green","blue"};
		for(int j=0;j<family.length;j++){
			for(int i=0;i<card_owned.size();i++){
				if(((City)card_owned.get(i)).family.equals(family[j])){
					if(j==0){
						nbCardPink++;
					}
					else if(j==1){
						nbCardCyan++;
					}
					else if(j==2){
						nbCardMagenta++;
					}
					else if(j==3){
						nbCardOrange++;
					}
					else if(j==4){
						nbCardRed++;
					}
					else if (j==5){
						nbCardYellow++;
					}
					else if(j==6){
						nbCardGreen++;
					}
					else {
						nbCardBlue++;
					}
				}
			}
		}
		if(nbCardPink==2){
			fullFamily.add("pink");
		}
		if(nbCardCyan==3){
			fullFamily.add("cyan");
		}
		if(nbCardMagenta==3){
			fullFamily.add("magenta");
		}
		if(nbCardOrange==3){
			fullFamily.add("orange");
		}
		if(nbCardRed==3){
			fullFamily.add("red");
		}
		if(nbCardYellow==3){
			fullFamily.add("yellow");
		}
		if(nbCardGreen==3){
			fullFamily.add("green");
		}
		if(nbCardBlue==2){
			fullFamily.add("blue");
		}
		return fullFamily;
	}

	//return the number of station owned
	public int getNbStation(){
		int nb=0;
		for(int i=0;i<card_owned.size();i++){
			if(((City)card_owned.get(i)).family.equals("station")){
				nb++;
			}
		}
		return nb;
	}

	//return all cards owned
	public ArrayList<Card> getAllCards(){
		return card_owned;
	}

	//return the number of utilities owned
	public int getNbUtility(){
		int nb=0;
		for(int i=0;i<card_owned.size();i++){
			if(((City)card_owned.get(i)).family.equals("utility")){
				nb++;
			}
		}
		return nb;
	}


	//setters
	public void setPosition (Cell c){
		this.position=c;
	}

	public void setMoney(int m){
		this.account+=m;
	}

	public void setFreeJail(int i){
		if(i==0)
			free_jail=i;
		else
			free_jail+=i;
	}


	public void pay(int m){
		this.account-=m;
	}

	public void setPrisoner() {
		prisoner=true;
	}
	public void setFree(){
		prisoner = false;
		try_espape=0;
	}

	public void setDouble(int d){
		double_done=d;
	}
	public void setTryEscape(int n){
		try_espape=n;
	}

	//add a card in the player game.
	public void setCard(City c){
		card_owned.add(c);
	}

	//remove a card of the player game
	public boolean removeCard(Card c){
		if(card_owned.contains(c)){
			card_owned.remove(c);
			return true;
		}
		return false;
	}
}